package pbc.gx.ekp.addressbook.service;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import pbc.gx.ekp.addressbook.dao.DeptDao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class DeptServiceImpl implements DeptService {
    private final DeptDao deptDao;

    public DeptServiceImpl(DeptDao deptDao) {
        this.deptDao = deptDao;
    }

    @Override
    public List<Map<String, Object>> getDeptRoot() {
        return deptDao.getDepts("");
    }

    @Override
    public Map<String, Object> getDepts(String parentId, Integer pageSize) {
        Map<String, Object> rsMap = new HashMap<>();
        List<Map<String, Object>> depts = deptDao.getDepts(parentId);
        rsMap.put("departmentArr", depts);
        // Map<String, Object> rsPersons = deptDao.getPersons(parentId, null, null, null, 0, pageSize);
        // rsMap.putAll(rsPersons);
        return rsMap;
    }

    @Override
    public Map<String, Object> getPersons(String departmentId, String department, String name, String phone, int pageNum, int pageSize) {
        if (StringUtils.isBlank(departmentId)) {
            return deptDao.getPersons(department, name, phone, pageNum, pageSize);
        } else {
            return deptDao.getPersons(departmentId, department, name, phone, pageNum, pageSize);
        }
    }

    @Override
    public List getDownloadPersons(String departmentId, String department, String name, String phone) {
        if (StringUtils.isBlank(departmentId)) {
            return deptDao.getDownloadPersons(department, name, phone);
        } else {
            return deptDao.getDownloadPersons(departmentId, department, name, phone);
        }
    }

    @Override
    public void updateDeptOrder() {
        deptDao.updateDeptOrder();
        log.info("updateDeptOrder");
    }
}
